package com.sk.weichat.ui.message.search;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SimpleItemAnimator;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.sk.weichat.AppConstant;
import com.sk.weichat.R;
import com.sk.weichat.adapter.SearchChatRecodeAdapter;
import com.sk.weichat.bean.Friend;
import com.sk.weichat.db.dao.ChatMessageDao;
import com.sk.weichat.ui.base.BaseActivity;
import com.sk.weichat.ui.message.ChatActivity;
import com.sk.weichat.ui.message.MucChatActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * 多条聊天/群聊记录
 */
public class MoreChatRecordActivity extends BaseActivity {

    private TextView tv_content;
    private RecyclerView recy_more_chat;

    private Friend friend;
    private String content;


    private List<Friend> mChatRecordList = new ArrayList<>();//聊天记录列表
    private SearchChatRecodeAdapter mChatRecordAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_more_chat_record);
        getSupportActionBar().hide();

        friend = (Friend) getIntent().getSerializableExtra(ChatActivity.FRIEND);
        content = getIntent().getStringExtra("content");
        findViewById(R.id.mergerStatus).setBackgroundColor(ContextCompat.getColor(this, R.color.white));
        findViewById(R.id.iv_title_left).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        TextView tvTitle = findViewById(R.id.tv_title_center);
        if (TextUtils.isEmpty(friend.getRemarkName())) {
            tvTitle.setText(friend.getNickName());
        } else {
            tvTitle.setText(friend.getRemarkName());
        }
        initView();
    }


    private void initView() {
        tv_content = findViewById(R.id.tv_content);
        tv_content.setText("共有" + friend.getContextNum() + "条与“" + content + "”有关的聊天记录");
        recy_more_chat = findViewById(R.id.recy_more_chat);
        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recy_more_chat.setLayoutManager(manager);

        mChatRecordList = ChatMessageDao.getInstance().queryChatMessageByContent(friend, content);//查找聊天记录
        mChatRecordAdapter = new SearchChatRecodeAdapter(mChatRecordList, false);
        mChatRecordAdapter.setKeyword(content);
        mChatRecordAdapter.setHasStableIds(true);
        recy_more_chat.setAdapter(mChatRecordAdapter);
        //禁止RecyclerView的默认动画
        ((SimpleItemAnimator) recy_more_chat.getItemAnimator()).setSupportsChangeAnimations(false);
        mChatRecordAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                Friend mfriend = mChatRecordList.get(position);
                Intent intent = null;
                if (friend.getRoomFlag() == 0) {//单聊记录
                    intent = new Intent(MoreChatRecordActivity.this, ChatActivity.class);
                    intent.putExtra(ChatActivity.FRIEND, mfriend);
                } else {//群聊记录
                    intent = new Intent(MoreChatRecordActivity.this, MucChatActivity.class);
                    intent.putExtra(AppConstant.EXTRA_USER_ID, mfriend.getUserId());
                    intent.putExtra(AppConstant.EXTRA_NICK_NAME, mfriend.getNickName());
                }
                intent.putExtra("isserch", true);
                intent.putExtra("jilu_id", mfriend.getChatRecordTimeOut());
                startActivity(intent);
            }
        });
    }
}
